#include "MainView.h"
// 图片资源
extern "C" {
	LV_IMG_DECLARE(ui_img_watch_face);
	LV_IMG_DECLARE(ui_img_arc);
	LV_IMG_DECLARE(ui_img_icon_png);
	LV_IMG_DECLARE(ui_img_s_line1_png);
	LV_IMG_DECLARE(ui_img_min_png);
	LV_IMG_DECLARE(ui_img_hour_png);
	LV_IMG_DECLARE(ui_img_dot_png);
	LV_IMG_DECLARE(ui_img_line_125_png);
}

void MainView::Create(lv_obj_t* body) {

	// body
	lv_obj_set_style_bg_img_src(body, &ui_img_watch_face, LV_PART_MAIN);

	CreateLeftTool(body);
	CreateUpTool(body);
	CreateDownTool(body);
	CreateRightTool(body);
	CreateClock(body);
}

void MainView::CreateLeftTool(lv_obj_t* parent) {

	lv_obj_t* ui_fitArc = lv_arc_create(parent);
	lv_obj_set_size(ui_fitArc, 148, 146);
	lv_obj_set_pos(ui_fitArc, -125, 0);
	lv_obj_set_align(ui_fitArc, LV_ALIGN_CENTER);
	lv_arc_set_value(ui_fitArc, 65);
	lv_arc_set_bg_angles(ui_fitArc, 138, 42);

	// 主背景色
	lv_obj_set_style_arc_width(ui_fitArc, 13, LV_PART_MAIN);
	lv_obj_set_style_arc_color(ui_fitArc, lv_color_hex(0x101418), LV_PART_MAIN);

	// 指示器样式
	lv_obj_set_style_arc_width(ui_fitArc, 13, LV_PART_INDICATOR);
	lv_obj_set_style_arc_rounded(ui_fitArc, true, LV_PART_INDICATOR);
	lv_obj_set_style_arc_img_src(ui_fitArc, &ui_img_arc, LV_PART_INDICATOR);

	// 滑块样式
	lv_obj_set_style_bg_opa(ui_fitArc, 0, LV_PART_KNOB);

	lv_obj_t* ui_fitIcon = lv_img_create(ui_fitArc);
	lv_img_set_src(ui_fitIcon, &ui_img_icon_png);
	lv_obj_set_size(ui_fitIcon, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
	lv_obj_set_align(ui_fitIcon, LV_ALIGN_CENTER);

	// 标签
	lv_obj_t* ui_fittitle = lv_label_create(ui_fitArc);
	lv_obj_set_size(ui_fittitle, LV_SIZE_CONTENT, LV_SIZE_CONTENT); 
	lv_obj_set_pos(ui_fittitle, 0, 55);
	lv_obj_set_align(ui_fittitle, LV_ALIGN_CENTER);
	lv_label_set_text(ui_fittitle, "VO2 Max");
	lv_obj_set_style_text_color(ui_fittitle, lv_color_hex(0x626562), LV_PART_MAIN);
}
void MainView::CreateUpTool(lv_obj_t* parent) {
	lv_obj_t* ui_UpTool = lv_obj_create(parent);
	lv_obj_set_size(ui_UpTool, 100, 102);
	lv_obj_set_pos(ui_UpTool, 0, - 125);
	lv_obj_set_align(ui_UpTool, LV_ALIGN_CENTER);
	lv_obj_set_style_bg_opa(ui_UpTool, 0, LV_PART_MAIN);

	lv_obj_t* ui_stepLine = lv_img_create(ui_UpTool);
	lv_img_set_src(ui_stepLine, &ui_img_s_line1_png);
	lv_obj_set_size(ui_stepLine, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
	lv_obj_set_pos(ui_stepLine, 0, -13);
	lv_obj_set_align(ui_stepLine, LV_ALIGN_CENTER);
	lv_img_set_pivot(ui_stepLine, 6, 39); // 轴心
	lv_img_set_angle(ui_stepLine, 300); // 角度

	lv_obj_t* ui_stepNum = lv_label_create(ui_UpTool);
	//lv_obj_set_size(ui_stepNum, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
	lv_obj_set_pos(ui_stepNum, 0, 34);
	lv_obj_set_align(ui_stepNum, LV_ALIGN_CENTER);
	lv_label_set_text(ui_stepNum, "80000");
	lv_obj_set_style_text_color(ui_stepNum, lv_color_white(), LV_PART_MAIN);
	lv_obj_set_style_text_font(ui_stepNum, &lv_font_montserrat_24, LV_PART_MAIN);
}
void MainView::CreateDownTool(lv_obj_t* parent) {
	lv_obj_t* ui_DownTool = lv_obj_create(parent);
	lv_obj_set_size(ui_DownTool, 73, 102);
	lv_obj_set_pos(ui_DownTool, -1, 128);
	lv_obj_set_align(ui_DownTool, LV_ALIGN_CENTER);
	lv_obj_clear_flag(ui_DownTool, LV_OBJ_FLAG_SCROLLABLE); // 去除滚动条
	lv_obj_set_style_bg_opa(ui_DownTool, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_DownTool, lv_color_hex(0x202020), LV_PART_MAIN);

	lv_obj_t* ui_carloLine = lv_img_create(ui_DownTool);
	lv_img_set_src(ui_carloLine, &ui_img_s_line1_png);
	lv_obj_set_size(ui_carloLine, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
	lv_obj_set_pos(ui_carloLine, 0, -16);
	lv_obj_set_align(ui_carloLine, LV_ALIGN_CENTER);
	lv_img_set_pivot(ui_carloLine, 6, 39);
	lv_img_set_angle(ui_carloLine, 1200);

	lv_obj_t* ui_Carlo = lv_label_create(ui_DownTool);
	lv_obj_set_pos(ui_Carlo, 0, 39);
	lv_obj_set_align(ui_Carlo, LV_ALIGN_CENTER);
	lv_label_set_text(ui_Carlo, "300");
	lv_obj_set_style_text_color(ui_Carlo, lv_color_white(), LV_PART_MAIN);
	lv_obj_set_style_text_font(ui_Carlo, &lv_font_montserrat_24, LV_PART_MAIN);
}
void MainView::CreateRightTool(lv_obj_t* parent) {

	// 容器
	lv_obj_t* ui_rightTool = lv_obj_create(parent);
	lv_obj_set_size(ui_rightTool, 69, 86);
	lv_obj_set_pos(ui_rightTool, 124, -1);
	lv_obj_set_align(ui_rightTool, LV_ALIGN_CENTER);
	lv_obj_clear_flag(ui_rightTool, LV_OBJ_FLAG_SCROLLABLE); 

	// 标签-月
	lv_obj_t* ui_calendarMon = lv_label_create(ui_rightTool);
	lv_obj_set_pos(ui_calendarMon, 0, -26);
	lv_obj_set_align(ui_calendarMon, LV_ALIGN_CENTER);
	lv_label_set_text(ui_calendarMon, "Apr");
	lv_obj_set_style_text_color(ui_calendarMon, lv_color_hex(0xF90606), LV_PART_MAIN);
	lv_obj_set_style_text_align(ui_calendarMon, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
	lv_obj_set_style_text_font(ui_calendarMon, &lv_font_montserrat_26, LV_PART_MAIN);

	// 标签-日
	lv_obj_t* ui_calendarDay = lv_label_create(ui_rightTool);
	lv_obj_set_pos(ui_calendarDay, 0, 24);
	lv_obj_set_align(ui_calendarDay, LV_ALIGN_CENTER);
	lv_label_set_text(ui_calendarDay, "29");
	lv_obj_set_style_text_align(ui_calendarDay, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN );
	lv_obj_set_style_text_font(ui_calendarDay, &lv_font_montserrat_46, LV_PART_MAIN);
}
void MainView::CreateClock(lv_obj_t* parent)
{
	ui_min = lv_img_create(parent);
	lv_img_set_src(ui_min, &ui_img_min_png);
	lv_obj_set_width(ui_min, LV_SIZE_CONTENT);  /// 1
	lv_obj_set_height(ui_min, LV_SIZE_CONTENT); /// 1
	lv_obj_set_x(ui_min, 0);
	lv_obj_set_y(ui_min, -100);
	lv_obj_set_align(ui_min, LV_ALIGN_CENTER);
	lv_obj_add_flag(ui_min, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
	lv_obj_clear_flag(ui_min, LV_OBJ_FLAG_SCROLLABLE); /// Flags
	lv_img_set_pivot(ui_min, 7, 206);

	ui_hour = lv_img_create(parent);
	lv_img_set_src(ui_hour, &ui_img_hour_png);
	lv_obj_set_width(ui_hour, LV_SIZE_CONTENT);  /// 1
	lv_obj_set_height(ui_hour, LV_SIZE_CONTENT); /// 1
	lv_obj_set_x(ui_hour, 0);
	lv_obj_set_y(ui_hour, -77);
	lv_obj_set_align(ui_hour, LV_ALIGN_CENTER);
	lv_obj_add_flag(ui_hour, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
	lv_obj_clear_flag(ui_hour, LV_OBJ_FLAG_SCROLLABLE); /// Flags
	lv_img_set_pivot(ui_hour, 7, 160);

	ui_sec = lv_img_create(parent);
	lv_img_set_src(ui_sec, &ui_img_line_125_png);
	lv_obj_set_width(ui_sec, LV_SIZE_CONTENT);  /// 1
	lv_obj_set_height(ui_sec, LV_SIZE_CONTENT); /// 1
	lv_obj_set_x(ui_sec, 0);
	lv_obj_set_y(ui_sec, -110);
	lv_obj_set_align(ui_sec, LV_ALIGN_CENTER);
	lv_obj_add_flag(ui_sec, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
	lv_obj_clear_flag(ui_sec, LV_OBJ_FLAG_SCROLLABLE); /// Flags
	lv_img_set_pivot(ui_sec, 8, 227);

	lv_obj_t* ui_dot = lv_img_create(parent);
	lv_img_set_src(ui_dot, &ui_img_dot_png);
	lv_obj_set_width(ui_dot, LV_SIZE_CONTENT);  /// 8
	lv_obj_set_height(ui_dot, LV_SIZE_CONTENT); /// 8
	lv_obj_set_align(ui_dot, LV_ALIGN_CENTER);
	lv_obj_add_flag(ui_dot, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
	lv_obj_clear_flag(ui_dot, LV_OBJ_FLAG_SCROLLABLE); /// Flags

}

void MainView::StartElemAnim() {

	// 动画时间线
	m_timeline = lv_anim_timeline_create();

	lv_anim_t hour_anim, sec_anim, min_anim;

	/* 时针-旋转（角度） */
	lv_anim_init(&hour_anim); // 初始化动画类型
	lv_anim_set_var(&hour_anim, ui_hour); // 使用自动变量存储
	lv_anim_set_exec_cb(&hour_anim, (lv_anim_exec_xcb_t)lv_img_set_angle); // 动画执行时的回调（动画需要改变什么属性）
	lv_anim_set_values(&hour_anim, 0, 300); // 属性改变的范围
	lv_anim_set_path_cb(&hour_anim, lv_anim_path_ease_out); // 动画效果
	lv_anim_set_time(&hour_anim, 1000); // 持续时间

	/* 分针 */
	lv_anim_init(&min_anim);
	lv_anim_set_var(&min_anim, ui_min);
	lv_anim_set_exec_cb(&min_anim, (lv_anim_exec_xcb_t)lv_img_set_angle);
	lv_anim_set_values(&min_anim, 0, 1400);
	lv_anim_set_path_cb(&min_anim, lv_anim_path_ease_out);
	lv_anim_set_time(&min_anim, 1000);

	/* 秒针 */
	lv_anim_init(&sec_anim);
	lv_anim_set_var(&sec_anim, ui_sec);
	lv_anim_set_exec_cb(&sec_anim, (lv_anim_exec_xcb_t)lv_img_set_angle);
	lv_anim_set_values(&sec_anim, 0, 3600);
	lv_anim_set_path_cb(&sec_anim, lv_anim_path_linear);
	lv_anim_set_time(&sec_anim, 36000);
	lv_anim_set_repeat_count(&sec_anim, LV_ANIM_REPEAT_INFINITE);

	// 添加动画到时间线上
	lv_anim_timeline_add(m_timeline, 0, &hour_anim);
	lv_anim_timeline_add(m_timeline, 0, &min_anim);
	lv_anim_timeline_add(m_timeline, 0, &sec_anim);

	// 执行时间线
	lv_anim_timeline_start(m_timeline);
}
